package pers.litao.bit.order.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pers.litao.bit.commons.entities.order.OrderEntity;
import pers.litao.bit.commons.entities.order.RetailWeekEntity;
import pers.litao.bit.commons.utils.*;
import pers.litao.bit.order.dao.RetailWeekMapper;
import pers.litao.bit.order.service.OrderService;
import pers.litao.bit.order.service.RetailWeekService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李涛
 * @date 2020-12-20 14:50
 */
@Service
@Slf4j
public class RetailWeekServiceImpl extends ServiceImpl<RetailWeekMapper, RetailWeekEntity> implements RetailWeekService {

    @Autowired
    private OrderService orderService;
    /**
     * 零售商周统计
     *
     * @return
     */
    @Override
    public R getWeek(LoginUserInfo loginUserInfo) {
        // 未登录
        if(null == loginUserInfo){
            log.error("用户未登录操作" + SysUtil.getClientInfo().buildInfo());
            return R.error(Constant.ResponseCode.TOKEM_INVALID.getCode(),
                    Constant.ResponseCode.TOKEM_INVALID.getMsg(), null);
        }
        // 只有供应商才有订单统计
        if (loginUserInfo.getIdentity() != 0) {
            log.error("身份验证错误"+loginUserInfo.info() + SysUtil.getClientInfo().buildInfo());
            return R.error(Constant.ResponseCode.RETAIL_API.getCode(),
                    Constant.ResponseCode.RETAIL_API.getMsg(), null);
        }
        DateTime date = DateUtil.date();
        List<OrderEntity> orderEntities = orderService.list(new QueryWrapper<OrderEntity>()
                .eq("retailers_id", loginUserInfo.getUserId())
                .ge("create_time", DateUtil.beginOfDay(DateUtil.offsetDay(date, -6)).getTime())
                .lt("create_time", DateUtil.beginOfDay(DateUtil.offsetDay(date, 1)).getTime())
                .lt("order_status", 3));
        List<RetailWeekEntity> retailWeek = new ArrayList<>();
        for(int i = 0 ; i >= -6;i-- ){
            long begin = DateUtil.beginOfDay(DateUtil.offsetDay(date, i)).getTime();
            long end = DateUtil.beginOfDay(DateUtil.offsetDay(date, i + 1)).getTime();
            BigDecimal buyMoney = orderEntities.stream()
                    .filter(orderEntity -> orderEntity.getCreateTime() >= begin && orderEntity.getCreateTime() < end)
                    .map(OrderEntity::getMoney)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal sellMoney = orderEntities.stream()
                    .filter(orderEntity -> orderEntity.getCreateTime() >= begin && orderEntity.getCreateTime() < end)
                    .map(order -> {
                        if (order.getIprice() == null) {
                            return BigDecimal.ZERO;
                        } else {
                            return order.getIprice().multiply(BigDecimal.valueOf(order.getSnum()));
                        }
                    })
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            RetailWeekEntity retailWeekEntity = new RetailWeekEntity();
            retailWeekEntity.setBuyMoney(buyMoney);
            retailWeekEntity.setSellMoney(sellMoney);
            retailWeek.add(retailWeekEntity);
        }
        log.info("用户请求成功" +loginUserInfo.info() + SysUtil.getClientInfo().buildInfo());
        return R.ok(retailWeek);
    }
}
